package com.server.netty.first;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author:lsx https://www.cnblogs.com/linjiqin/p/10117382.html
 * @description: netty服务端
 * @date:2020/3/3
 */
public class NettyServer {
    public static void main(String[] args) throws Exception {
        //第1步定义两个线程组，用来处理客户端通道的accept和读写事件
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        EventLoopGroup childGroup = new NioEventLoopGroup();
        try {
            //首先，netty通过ServerBootstrap启动服务端
            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class)
                    //子处理器
                    .childHandler(new ServerInitializer());
            ChannelFuture future = bootstrap.bind(8899).sync();
            future.channel().closeFuture().sync();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }
}
